﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using LotteryVote.Entity;
using LotteryVote.Utility;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote.Logic.Web
{
    public class BaseControl : UserControl, IExecutable
    {
        private LoginFacade _loginFacade;
        private User _user;

        protected LoginFacade LoginFacade
        {
            get
            {
                if (_loginFacade == null)
                    _loginFacade = new LoginFacade();
                return _loginFacade;
            }
        }
        public User CurrentUser
        {
            get
            {
                if (_user == null)
                    _user = LoginFacade.GetCurrentUser();
                return _user;
            }
        }
        public bool IsLogin
        {
            get { return LoginFacade.IsLogin; }
        }

        public void Execute(Action action)
        {
            try
            {
                action();
            }
            catch (SameNameUserException ex)
            {
                LogConsole.Error(ex);
                ShowMessage("Error!", "已存在用户" + ex.Message);
            }
            catch (InvalidShareRateException ex)
            {
                LogConsole.Error(ex);
                ShowMessage("error", ex.Message);
            }
            catch (InvalidGivenCreditException ex)
            {
                LogConsole.Error(ex);
                ShowMessage("Error", string.Format("输入{0},实际可用{1}", ex.GivenCredit, ex.AvailableGivenCredit));
            }
            catch (PromptException ex)
            {
                LogConsole.Error(ex);
                ShowMessage("Error!", string.Format(ex.Message, ex.ErrorParams));
            }
            catch (NoPermissionException ex)
            {
                LogConsole.Error(Request.RawUrl + "无权限访问! CurrentUser:" + CurrentUser.ToString());
                SystemErrorLog.LogError(ex, ErrorLevel.User, Request.RawUrl);
                Response.Redirect("~/NoPermissions.aspx");
            }
            catch (InvalidRoleException ex)
            {
                LogConsole.Error(Request.RawUrl + "权限异常!  CurrentUser:" + CurrentUser.ToString());
                SystemErrorLog.LogError(ex, ErrorLevel.User, Request.RawUrl);
            }
            catch (InvalidDataException ex)
            {
                LogConsole.Error(Request.RawUrl + "数据异常! CurrentUser:" + CurrentUser.ToString());
                SystemErrorLog.LogError(ex, ErrorLevel.InvalidData, Request.RawUrl);
            }
            catch (ArgumentException ex)
            {
                LogConsole.Error(Request.RawUrl + " 处理发生系统异常!");
                SystemErrorLog.LogError(ex, ErrorLevel.InvalidData, Request.RawUrl);
            }
            catch (Exception ex)
            {
                LogConsole.Error(Request.RawUrl + " 处理发生异常!");
                SystemErrorLog.LogError(ex, ErrorLevel.Application, Request.RawUrl);
            }
        }

        protected void ShowMessage(string title, string contect)
        {
            string script = string.Format("showMessage(\"{0}\",\"{1}\");", title, contect);
            this.Page.ClientScript.RegisterStartupScript(this.GetType(), title, script, true);
        }


        public T Execute<T>(Func<T> action)
        {
            try
            {
                return action();
            }
            catch (SameNameUserException ex)
            {
                LogConsole.Error(ex);
                ShowMessage("Error!", "已存在用户" + ex.Message);
            }
            catch (InvalidShareRateException ex)
            {
                LogConsole.Error(ex);
                ShowMessage("error", ex.Message);
            }
            catch (InvalidGivenCreditException ex)
            {
                LogConsole.Error(ex);
                ShowMessage("Error", string.Format("输入{0},实际可用{1}", ex.GivenCredit, ex.AvailableGivenCredit));
            }
            catch (PromptException ex)
            {
                LogConsole.Error(ex);
                ShowMessage("Error!", string.Format(ex.Message, ex.ErrorParams));
            }
            catch (NoPermissionException ex)
            {
                LogConsole.Error(Request.RawUrl + "无权限访问! CurrentUser:" + CurrentUser.ToString());
                SystemErrorLog.LogError(ex, ErrorLevel.User, Request.RawUrl);
                Response.Redirect("~/NoPermissions.aspx");
            }
            catch (InvalidRoleException ex)
            {
                LogConsole.Error(Request.RawUrl + "权限异常!  CurrentUser:" + CurrentUser.ToString());
                SystemErrorLog.LogError(ex, ErrorLevel.User, Request.RawUrl);
            }
            catch (InvalidDataException ex)
            {
                LogConsole.Error(Request.RawUrl + "数据异常! CurrentUser:" + CurrentUser.ToString());
                SystemErrorLog.LogError(ex, ErrorLevel.InvalidData, Request.RawUrl);
            }
            catch (ArgumentException ex)
            {
                LogConsole.Error(Request.RawUrl + " 处理发生系统异常!");
                SystemErrorLog.LogError(ex, ErrorLevel.InvalidData, Request.RawUrl);
            }
            catch (Exception ex)
            {
                LogConsole.Error(Request.RawUrl + " 处理发生异常!");
                SystemErrorLog.LogError(ex, ErrorLevel.Application, Request.RawUrl);
            }
            Response.Redirect("~/Error.aspx");
            return default(T);
        }
    }
}
